package iOperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-06-03
 * Time: 12:48
 */
public class AddIOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("增添书籍");
        //判满
        int currentSize = bookList.getUsedSize();
        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架已满，添加失败");
            return;
        }
        System.out.println("请输入图书名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入图书的价格：");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入图书的类型：");
        String type = scanner.nextLine();

        Book newBook = new Book(name,author,price,type);

        //判断书架有没有这本书
        for (int i = 0; i < bookList.getBooks().length; i++) {
            if(newBook.equals(bookList.getBooks()[i])) {
                System.out.println("该书已存在，添加无效");
                return;
            }
        }
        bookList.setBooks(currentSize,newBook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("添加成功");
    }
}
